from iClouds.allSerializers import *

from .Serializers import *

# Create your views here.
class PayJingdianListModelViewSet(viewsets.ModelViewSet):
    __title__ = "景点列表"
    __group__ = "景区模板"
    __info__ = {"get": "景点列表", "post": "创建景点", "patch": "编辑景点", "delete": "移除景点"}
    __doc__ = """	list:
    <h4>景点列表</h4>
    retrieve:
    <h4>景点详情</h4>
    create:
    <h4>景点创建</h4>
        ?version=1
    partial_update:
    <h4>景点编辑</h4>
    delete:
    <h4>景点移除</h4>"""
    http_method_names = ["get", "post", "patch"]
    authentication_classes = []
    permission_classes = [AllPermissions]
    versioning_class = MyDefaultVersioning
    serializer_class = NoneSerializer
    queryset = PayJingdianList.objects.all().order_by("id")

    # filterset_class = CallmeListFilterSet
    filter_backends = [MyFilterBackend, MySearchBackend, MyOrderBackend]
    filterset_fields = ["status", "parent"]
    # search_fields = ['phone','email']
    ordering_fields = ["id"]

    pagination_class = StandardPagination

    def get_serializer_class(self):
        # if self.request and self.request.version == '1':
        #     return NewStaffUserbaseListSerializer
        return PayJingdianListSerializer

    def list(self, request, *args, **kwargs):
        queryset = self.filter_queryset(self.get_queryset())
        c = queryset.count()
        page = self.paginate_queryset(queryset)
        serializer = self.get_serializer(page, many=True)
        return Response(
            {
                "rescode": 200,
                "resmsg": serializer.data,
                "count": c,
                "size": self.pagination_class.page_size,
            }
        )

    def retrieve(self, request, *args, **kwargs):
        instance = self.get_object()
        serializer = self.get_serializer(instance)
        return Response({"rescode": 200, "resmsg": serializer.data})

    def create(self, request, *args, **kwargs):
        serializer = self.serializer_class(data=request.data)
        serializer.is_valid(raise_exception=True)
        serializer.save()
        return Response({"rescode": 200, "resmsg": serializer.data})

    def update(self, request, *args, **kwargs):
        instance = self.get_object()
        serializer = self.get_serializer(instance, data=request.data)
        serializer.is_valid(raise_exception=True)
        serializer.save()
        return Response({"rescode": 200, "resmsg": serializer.data})

    def partial_update(self, request, *args, **kwargs):
        partial = True
        instance = self.get_object()
        serializer = self.get_serializer(instance, data=request.data, partial=partial)
        serializer.is_valid(raise_exception=True)
        serializer.save()
        return Response({"rescode": 200, "resmsg": serializer.data})

    def destroy(self, request, *args, **kwargs):
        instance = self.get_object()
        self.perform_destroy(instance)
        return Response({"rescode": 200, "resmsg": "SUCCESS"})
